<template>
  <div class="boss_login">
    <div class="boss-content">
      <img class="login-bg" src="../../assets/images/login.png" />

      <div class="login-form">
        <div class="login-form-logo">
          <img src="../../assets/images/yegoologo.png" />
          <span>仓库登录</span>
        </div>
        <el-form class="loginForm" ref="loginFormRef" @keyup.enter.native="onLoginClick" :show-message="false">
          <el-form-item prop="userName">
            <el-input v-model="form.userName" class="boss_input" placeholder="账号" clearable>
              <template #prefix>
                <img src="../../assets/images/username.png" class="icon" />
              </template>
            </el-input>
          </el-form-item>

          <el-form-item prop="password">
            <el-input v-model="form.password" class="boss_input" placeholder="密码" show-password clearable>
              <template #prefix>
                <img src="../../assets/images/password.png" class="icon" />
              </template>
            </el-input>
          </el-form-item>
          <el-alert v-if="form.errMessage" type="error" :closable="false">
            <template #default>
              {{ form.error }}
            </template>
          </el-alert>
          <el-form-item>
            <el-button type="primary" class="login-button" @click="onLoginClick" :loading="form.loading">登 录
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive } from 'vue'
import { userStore } from '@/store/user'
import { useRouter } from 'vue-router'
import api from '@/api'
import md5 from 'js-md5'

const router = useRouter()
const _userStore = userStore()

const form = reactive({
  userName: '',
  password: '',
  errMessage: '',
  loading: false,
  error: ''
})
const onLoginClick = async () => {
  const url = '/retrospect/login'
  form.loading = false
  let { data, err } = await api.post(url, {
    userName: form.userName,
    password: md5(form.password)
  })
  if (err) {
    form.errMessage = true
    form.error = err
  } else {
    const employee = data.warehouseUser
    _userStore.login(employee)
    router.push('/')
  }
}
</script>

<style lang="scss" scoped>
.boss_login {
  background-image: linear-gradient(-45deg, #40d6ff 0%, #409eff 100%);
  min-height: calc(100% - 275px);

  .boss-content {
    width: 1080px;
    margin: 0 auto;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .login-bg {
      display: inline-block;
      width: 588px;
      height: 558px;
    }

    .login-form {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      padding: 50px 35px;
      background-color: var(--background);
      box-shadow: 0px 0px 10px 5px rgba(72, 81, 90, 0.05);
      border-radius: 8px;

      .login-form-logo {
        display: flex;
        flex-direction: column;
        align-items: center;

        img {
          width: 70px;
          height: 70px;
        }

        span {
          margin-top: 15px;
          font-size: 16px;
          color: var(--color-text);
          letter-spacing: 0;
          text-align: justify;
          font-weight: 500;
        }
      }

      .loginForm {
        .el-form-item {
          margin-bottom: 0 !important;
        }

        .boss_input {
          width: 280px;
          margin-top: 20px;
          border: 1px solid var(--input-border-color);
          border-radius: 20px;

          .icon {
            width: 16px;
            height: 16px;
          }
        }
      }

      .el-alert--error.is-light {
        margin-top: 10px;
        border-radius: 10px;
      }

      .login-button {
        width: 100%;
        padding: 20px 0;
        margin: 20px 0 12px 0;
        box-sizing: border-box;
        background: var(--color-primary);
        border-radius: 20px;
        border: 0;
        font-size: 16px;
        color: var(--background);
        font-weight: 500;
      }
    }
  }
}
</style>
<style lang="scss">
.loginForm {
  .el-input__wrapper,
  .el-input__wrapper.is-focus {
    box-shadow: none;
    background: none;
    border: 0;
  }

  .el-form-item.is-error {
    .boss_input {
      border: 0 !important;
    }

    .el-input__wrapper,
    .el-input__wrapper.is-focus {
      border-radius: 20px;
      box-shadow: 0 0 0 1px red inset;
    }
  }
}
</style>
